﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace PiControls.Controls.DriveUI.TCPClient
{
    public partial class ConfigTcpClientUi : UserControl
    {
        private string objectName;

        // 定义一个带有字符串参数的委托
        public delegate void SaveCurrConfigClickedEventHandler(object sender, string objectName, string ip, int port,  bool isAutoConnect, bool isConnected);
        // 声明事件
        public event SaveCurrConfigClickedEventHandler ButtonSaveCurrConfigClickedEventHandlerClicked;
        public delegate void stateConnectEventHandler(object sender, string objectName, string ip, int port, bool isAutoConnect, bool isConnected);
        public event stateConnectEventHandler stateConnectEventHandlerHandlerClicked;


        private bool isConnected = false;



        public ConfigTcpClientUi(string objectName,string ip, int port,bool isConnected,bool isAutoConnect)
        {
            InitializeComponent();
            this.textBoxIp.Text = ip;
            this.textBoxPort.Text = port.ToString();
            this.labelState.BackColor = isConnected ? Color.Green : Color.Red;
            this.isConnected = isConnected;
            this.piSwitchIisAutoConnect.L_IsOpen = isAutoConnect;
            this.objectName = objectName;
        }

        public void setStateOpen(bool isOpen)
        {
            this.labelState.BackColor = isOpen? Color.Green : Color.Red;

            this.isConnected = isOpen;
            if (isOpen)
            {
                this.buttonOpen.Text = "关闭连接";
            }
            else
            {
                this.buttonOpen.Text = "连接";
            }
        }
        

        private void ConfigTcpClientUi_Load(object sender, EventArgs e)
        {
        }

        private void buttonSaveCurrConfig_Click(object sender, EventArgs e)
        {
            ButtonSaveCurrConfigClickedEventHandlerClicked?.Invoke(this, objectName, textBoxIp.Text, int.Parse(textBoxPort.Text), piSwitchIisAutoConnect.L_IsOpen, isConnected);
        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void buttonOpen_Click(object sender, EventArgs e)
        {

            stateConnectEventHandlerHandlerClicked?.Invoke(this, objectName, textBoxIp.Text, int.Parse(textBoxPort.Text), piSwitchIisAutoConnect.L_IsOpen, isConnected);
        }
    }
}
